package com.momo.hotel.controller;

import com.momo.hotel.dao.pojo.*;
import com.momo.hotel.dao.pojo.ext.TbEvaluationExt;
import com.momo.hotel.dao.pojo.ext.TbRoomList;
import com.momo.hotel.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class EvaluationController {

    @Autowired
    EvaluationService evaluationService;

    /**
     * 添加评价
     * @param request
     * @return
     */
    @RequestMapping("addEvaluation")
    public void addEvaluation(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        //获取当前用户
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user!=null){
        //评论的房间id
        TbRoomList tbRoomDetail = (TbRoomList) request.getSession().getAttribute("tbRoomDetail");
        Integer roomID = tbRoomDetail.getId();
        //评论时间
        Date date = new Date();
        //评论内容
        String comment_content = request.getParameter("comment_content");
        TbEvaluation tbEvaluation = new TbEvaluation();
        tbEvaluation.setUserId(user.getId());
        tbEvaluation.setRoomId(roomID);
        tbEvaluation.setTime(date);
        tbEvaluation.setContent(comment_content);
        if(comment_content != ""){
            Integer integer = evaluationService.addEvaluation(tbEvaluation);
            if(integer != 0){
                response.getWriter().write("评价成功");
            }else {
                response.getWriter().write("评价失败");
            }
        }else {
            response.getWriter().write("评价内容不能为空");
        }
        }else {
            response.getWriter().write("请登陆");
        }
    }

    /**
     * 根据房间ID查询对应的评论
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("selectEvaluations")
    public List<TbEvaluationExt> selectEvaluations(HttpServletRequest request){
        TbRoomDetail tbRoomDetail = (TbRoomDetail) request.getSession().getAttribute("tbRoomDetail");
        Integer roomId = tbRoomDetail.getId();
        List<TbEvaluationExt> tbEvaluationExts = evaluationService.selectEvaluationById(roomId);
        return tbEvaluationExts;
    }
}
